import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:photo_view/photo_view.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
class NetPhotoViewSimpleScreen extends StatefulWidget {
  NetPhotoViewSimpleScreen({
    this.imagePaths,//图片
    this.loadingChild,//加载时的widget
    this.backgroundDecoration,//背景修饰
    this.minScale,//最大缩放倍数
    this.maxScale,//最小缩放倍数
    this.heroTag,//hero动画tagid
    this.curIndex,
  });
  final List<String>? imagePaths;
  final Widget? loadingChild;
  final Decoration? backgroundDecoration;
  final dynamic minScale;
  final dynamic maxScale;
  final String? heroTag;
  int? curIndex;
  @override
  _NetPhotoViewSimpleScreenState createState() => _NetPhotoViewSimpleScreenState();
}

class _NetPhotoViewSimpleScreenState extends State<NetPhotoViewSimpleScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        constraints: BoxConstraints.expand(
          height: MediaQuery.of(context).size.height,
        ),
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 0,
              left: 0,
              bottom: 0,
              right: 0,
              child: Swiper(
                index: widget.curIndex!,
                itemBuilder: (BuildContext context, int index){
                  return PhotoView(
                    imageProvider: NetworkImage(widget.imagePaths![index]),
                    // loadingChild: loadingChild,
                    backgroundDecoration: widget.backgroundDecoration as BoxDecoration?,
                    minScale: widget.minScale,
                    maxScale: widget.maxScale,
                    heroAttributes: PhotoViewHeroAttributes(tag: widget.heroTag!),
                    enableRotation: true,
                  );
                },
                itemCount: widget.imagePaths!.length,
                onIndexChanged: (index){
                  setState(() {
                    widget.curIndex = index;
                  });
                },
              ),
            ),
            Positioned(
                bottom: 20.0.hDp,
                left: 10.0.wDp,
                right: 10.0.wDp,
                child: Text("${widget.curIndex!+1}/${widget.imagePaths!.length}",style: TextStyle(color: Colors.white),)),
            Positioned(//右上角关闭按钮
              right: 10,
              top: MediaQuery.of(context).padding.top,
              child: Container(
                height: 30.0.wDp,
                width: 30.0.wDp,
                child: IconButton(
                  icon: Icon(Icons.close,size: 30,color: Colors.white,),
                  onPressed: (){
                    Navigator.of(context).pop();
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
